package com.cxq.boot.strategy;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserStrategyFactory {
    //注入的某个bean或属性是某个接口的子类，那么会被全部自动注入，如 GiteeUserStrategy、GithubUserStrategy 全被注入
    //前提：需要子类先注入spring容器中才能注入接口集合
    @Autowired
    private List<UserStrategy> strategies;

    public UserStrategy getStrategy(String type) {
        for (UserStrategy strategy : strategies) {
            //StringUtils是Commons lang下包装好的工具类
            if (StringUtils.equals(strategy.getSupportedType(),type)){
                return strategy;
            }
        }
        return null;
    }
}
